<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var carPooling = function (trips, capacity) {
      // [num_passengers, start_location, end_location]
      // [0, 1, 2, 3, 4]
      // [1, 2) -> [1, 1]
      let diff = new Array(1002).fill(0);
      let sum = 0;
      for (let [n, s, e] of trips) {
        diff[s] += n;
        diff[e] -= n; // e - 1 + 1
      }
      for (let i = 0; i < diff.length; i++) {
        sum += diff[i];
        if (sum > capacity) return false;
      }
      return true;
    };
    console.log(carPooling([[2, 1, 5], [3, 3, 7]], 4))
  </script>
</body>

</html>